package com.yy.okhttp.chain;

import com.yy.okhttp.Call;
import com.yy.okhttp.Interceptor;
import com.yy.okhttp.InterceptorChain;
import com.yy.okhttp.Response;
import com.yy.okhttp.YYOkHttpClient;

import java.io.IOException;

/**
 * User: gongyongfeng2@yy.com
 * Date: 2020/7/29 16 :35
 */
public class RetryInterceptor implements Interceptor {

    @Override
    public Response intercept(InterceptorChain chain) throws IOException {
        Call call = chain.call();
        YYOkHttpClient okHttpClient = call.client();
        IOException exception = null;
        for (int i = 0; i < okHttpClient.reTry(); i++) {
            if (call.isCanceled()) {
                throw new IllegalStateException("cancel");
            }
            try {
                Response response = chain.proceed();
                return response;
            } catch (IOException e) {
                exception = e;
            }

        }
        throw exception;
    }
}
